package com.xzqwjw.taskmanager.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.leaderinfo.main.ActivityShowInfo;
import com.xzqwjw.taskmanager.view.ListItemView;
import com.leaderinfo.leaderinfojxyz.R;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

@SuppressLint("InflateParams")
public class ListItemFavoriteAdapter extends BaseAdapter {

	private LayoutInflater objLInflater;
	private List<Map<String, Object>> listData = new ArrayList<Map<String, Object>>();
	private Context objContext;
	private String strDBFile;

	public ListItemFavoriteAdapter(Context objContext,
                                 List<Map<String, Object>> listData, String strDBFile) {
		this.objContext = objContext;
		this.strDBFile = strDBFile;
		this.objLInflater = (LayoutInflater) objContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.listData = listData;
	}

	@Override
	public Object getItem(int intPosition) {
		if (listData != null) {
			return listData.get(intPosition);
		} else {
			return null;
		}
	}

	@Override
	public long getItemId(int intPosition) {
		return intPosition;
	}

	@Override
	public int getCount() {
		if (listData != null) {
			return listData.size();
		} else {
			return 0;
		}
	}

	@Override
	public View getView(final int intPosition, View viewConvert,
			ViewGroup vgParent) {
		if (listData == null) {
			return null;
		}

		final ListItemView objVLI;

		if (viewConvert == null) {
			objVLI = new ListItemView();
			viewConvert = objLInflater.inflate(R.layout.item_list_favorite,
					null);
			objVLI.llItem = (LinearLayout) viewConvert
					.findViewById(R.id.llayoutItem);
			objVLI.tvID = (TextView) viewConvert.findViewById(R.id.tvID);
			objVLI.imgbtnHeadImg = (ImageButton) viewConvert
					.findViewById(R.id.imgbtnHeadPhoto);
			objVLI.tvName = (TextView) viewConvert.findViewById(R.id.tvName);
			objVLI.tvSex = (TextView) viewConvert.findViewById(R.id.tvSex);
			objVLI.tvBirthday = (TextView) viewConvert
					.findViewById(R.id.tvBirthday);
			objVLI.tvBirthplace = (TextView) viewConvert
					.findViewById(R.id.tvBirthplace);
			objVLI.tvUnitJob1 = (TextView) viewConvert
					.findViewById(R.id.tvUnitjob1);
			objVLI.tvUnitJob2 = (TextView) viewConvert
					.findViewById(R.id.tvUnitjob2);
			objVLI.imgbtnFavorite = (ImageButton) viewConvert
					.findViewById(R.id.imgbtnFavorite);
			viewConvert.setTag(objVLI);
		} else {
			objVLI = (ListItemView) viewConvert.getTag();
		}

		if (intPosition % 2 == 0) {
			objVLI.llItem.setBackgroundColor(Color.parseColor("#DFF0FF"));
		} else {
			objVLI.llItem.setBackgroundColor(Color.parseColor("#C4E2FF"));
		}

		final String strAID = (String) listData.get(intPosition).get("AID");

		objVLI.tvID.setText(strAID);

		String strPhotoPath = (String) listData.get(intPosition).get("Photo");
		Bitmap bmpPhoto = BitmapFactory.decodeFile(strPhotoPath, null);
		objVLI.imgbtnHeadImg
				.setImageBitmap(Utility.zoomBMP(bmpPhoto, 120, 160));

		objVLI.imgbtnHeadImg.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String strAID = (String) objVLI.tvID.getText();
				Intent objIT = new Intent(objContext, ActivityShowInfo.class);
				objIT.putExtra("AID", strAID);
				objIT.putExtra("DBFilePath", strDBFile);
				objContext.startActivity(objIT);
			}
		});

		objVLI.tvName.setText((String) listData.get(intPosition)
				.get("RealName"));

		String strSex = (String) listData.get(intPosition).get("Sex");
		objVLI.tvSex.setText("��" + strSex + "��");

		objVLI.tvBirthday.setText((String) listData.get(intPosition).get(
				"BirthdayAge"));
		objVLI.tvBirthplace.setText((String) listData.get(intPosition).get(
				"NativePlace"));
		objVLI.tvUnitJob1.setText((String) listData.get(intPosition).get(
				"UnitJob"));
		objVLI.tvUnitJob2.setText("");
		// objVLI.tvUnitJob2.setText((String)
		// listData.get(intPosition).get("UnitJob"));

		final String strIsFavorite = (String) listData.get(intPosition).get(
				"IsFavorite");
		if (strIsFavorite.equals("0")) {
			objVLI.imgbtnFavorite
					.setImageResource(R.drawable.bgbtn_favorite_add);
		} else if (strIsFavorite.equals("1")) {
			objVLI.imgbtnFavorite
					.setImageResource(R.drawable.bgbtn_favorite_cancel);
		}
		objVLI.imgbtnFavorite.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (strIsFavorite.equals("1")) {
					Dialog objDialog = new AlertDialog.Builder(objContext)
							.setTitle("ȡ���ղ�")
							.setIcon(R.drawable.ic_launcher)
							.setMessage("ȷ��Ҫȡ������Ա���ղ���")
							// �൱�ڵ��ȷ�ϰ�ť
							.setPositiveButton("ȷ��",
									new DialogInterface.OnClickListener() {
										@Override
										public void onClick(
												DialogInterface dialog,
												int which) {
											com.leaderinfo.dao.PartyInfoDao objDaoPartyInfo = new com.leaderinfo.dao.PartyInfoDao(
													strDBFile);
											if (objDaoPartyInfo
													.setFavorite(Integer
															.parseInt(strAID)) > 0) {
												listData.remove(intPosition);
												notifyDataSetChanged();
											}
										}
									})
							// �൱�ڵ��ȡ����ť
							.setNegativeButton("ȡ��",
									new DialogInterface.OnClickListener() {
										@Override
										public void onClick(
												DialogInterface dialog,
												int which) {
										}
									}).create();
					objDialog.show();
				}

			}
		});

		return viewConvert;
	}
}
